package com.chenfy.everysound

import android.content.Intent
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.chenfy.everysound.base.BaseActivity
import com.chenfy.everysound.entity.SoundData
import com.chenfy.everysound.net.ISoundService
import com.chenfy.everysound.net.ServiceCreator
import kotlinx.coroutines.launch

class BootActivity:BaseActivity() {
    override fun layout() = R.layout.activity_boot
    private val model by viewModels<BootViewModel>()

    override fun initData() {
        super.initData()
        model.initData()
        launch {
            val soundService = ServiceCreator.create<ISoundService>()
            val sounds = soundService.getSounds()
            SoundData.soundEntity = sounds
            startMain()
        }
    }

    private fun startMain(){
        val intent = Intent(this,MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    class BootViewModel: ViewModel(){
        fun initData(){
            viewModelScope.launch {
                val service = ServiceCreator.create<ISoundService>()
                val sounds = service.getSounds()
                Log.d("Chen",sounds.toString())
            }
        }
    }
}